package com.education.wisdom.controller.app;


import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.education.common.config.BusinessException;
import com.education.common.config.CommonErrorCode;
import com.education.common.core.BaseController;
import com.education.common.pojo.userData;
import com.education.common.utils.*;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.mapper.JrVersionMapper;
import com.education.wisdom.pojo.*;
import com.education.wisdom.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.*;

/**
 * 目录体系 信息操作处理
 *
 * @author ruoyi
 * @date 2018-11-15
 */
@RestController
@CrossOrigin
@Slf4j
@Api(tags = {"课程目录体系信息"})
@RequestMapping("/appCatalog")
public class AppCatalogController extends BaseController {


    @Autowired
    private ICatalogService catalogService;

    @Autowired
    private ICatalogUserLikeService catalogUserLikeService;

    @Autowired
    private ICommentsService commentsService;

    @Autowired
    private IPaperService paperService;

    @Autowired
    private IResourcesService resourcesService;

    @Autowired
    private IUserCatalogBuyService userCatalogBuyService;

    @Autowired
    private JrVersionMapper versionMapper;

    @Autowired
    private IMicrolessonService microlessonService;

    @Autowired
    private IBbsService bbsService;

    @Autowired
    private ILabelService labelService;

    @Autowired
    private ICatalogUserService catalogUserService;

    @Autowired
    private UserFeign userFeign;


    /**
     * 教学云课堂--虚拟教学
     */
    @GetMapping("/selectVirtual")
    @ResponseBody
    public ApiReturnObject selectVirtual(Integer catalogId) {
        List<Map<String, Object>> list = new ArrayList<>();
        //2 3D  9 flash
        List<Resources> three = resourcesService.selectResourcesByCatalogIdAndType(catalogId, 8);
        List<Resources> flash = resourcesService.selectResourcesByCatalogIdAndType(catalogId, 9);
        for (Resources resources : three) {
            Map<String, Object> map = new HashMap<>();
            map.put("threeD", resources.getrUrl());
            map.put("name", resources.getrName());
            for (Resources flash1 : flash) {
                map.put("flash", flash1.getrUrl());
            }
            list.add(map);
        }
        return new ApiReturnObject("200", "success", list);
    }

    /**
     * 教学云课堂--添加课程表
     */
    @GetMapping("/insertCurriculum")
    @ResponseBody
    public ApiReturnObject insertCurriculum(Integer catalogId, String userId) {
        if (catalogId == null) {
            throw new BusinessException(CommonErrorCode.E_200101);
        }

        //查询课程表是否有该课程
        Integer catalogUser = getCatalogUser(catalogId, userId);
        if (catalogUser > 0) {
            Integer i = getCatalogUser(catalogId, userId);
            if (i == 0) {
                throw new BusinessException(CommonErrorCode.E_300105);
            }
            catalogUserService.deleteCatalogIdAndUserId(catalogId, Integer.parseInt(userId));
            return new ApiReturnObject("200", "success", "取消成功");
        } else {
            CatalogUser catalogUser1 = new CatalogUser();
            catalogUser1.setUserId(userId);
            catalogUser1.setCatalogId(catalogId);
            catalogUser1.setCreateTime(new Date());
            catalogUserService.insertCatalogUser(catalogUser1);
            return new ApiReturnObject("200", "success", "添加成功");
        }
    }

    //查询课程表是否有该课程
    private Integer getCatalogUser(Integer catalogId, String userId) {
        CatalogUser catalogUser = new CatalogUser();
        catalogUser.setCatalogId(catalogId);
        catalogUser.setUserId(userId);
        List<CatalogUser> catalogUserList = catalogUserService.selectCatalogUserList(catalogUser);
        return catalogUserList.size();
    }

    /**
     * 教学云课堂--目录筛选
     */
    @GetMapping("/screening")
    @ResponseBody
    public ApiReturnObject screening(Integer catalogId) {
        if (catalogId == null) {
            throw new BusinessException(CommonErrorCode.E_200101);
        }
        List<Catalog> catalogs = new ArrayList<>();
        recursive(catalogId, catalogs);
        //设置标签
        for (Catalog catalog : catalogs) {
            List<Label> labels = labelService.selectLabelByCatalogId(catalog.getCatalogId());
            catalog.setLabelList(labels);
        }
        return new ApiReturnObject("200", "success", catalogs);
    }

    //递归查询课程--目录筛选使用
    public void recursive(Integer catalogId, List<Catalog> catalogs) {
        List<Catalog> catalogList = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : catalogList) {
            if (catalogList.size() != 0) {
                //只有第三级才要添加进去
                List<Catalog> list = catalogService.selectByParentIdAndSort(catalog.getCatalogId(), 3);
                if (list.size() != 0) {
                    for (Catalog catalog1 : list) {
                        catalogs.add(catalog1);
                    }
                } else {
                    recursive(catalog.getCatalogId(), catalogs);
                }
            }
        }
    }

    /**
     * 教学云课堂--查询目录
     */
    @GetMapping("/slectMenu")
    @ResponseBody
    public ApiReturnObject slectMenu(Integer catalogId) {
        if (catalogId == null) {
            catalogId = 0;
        }
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        return new ApiReturnObject("200", "success", list);
    }

    /**
     * 教学云课堂--查询全部课程
     */
    @GetMapping("/slectList")
    @ResponseBody
    public ApiReturnObject slectList(Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Catalog> list = catalogService.selectBySort(3);
        PageInfo<Catalog> pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "success", pageInfo);
    }

//    @GetMapping("/slectList")
//    @ResponseBody
//    public ApiReturnObject slectList(Integer pageNum, Integer pageSize) {
//        if (pageNum == null || pageSize == null) {
//            throw new BusinessException(CommonErrorCode.E_200106);
//        }
//        //当redis不为空 直接从redis拿数据
//        List redisCatalogs = (List) redisTemplate.opsForValue().get("catalogList");
//        if (redisCatalogs != null) {
//            return paging(pageNum, pageSize, redisCatalogs);
//        }
//
//        //存储所有最后一级课程
//        List<Catalog> catalogs = new ArrayList<>();
//        //递归查询课程
//        recursive(0, catalogs);
//        //查询课程标签
//        for (Catalog catalog : catalogs) {
//            List<Label> labels = labelService.selectLabelByCatalogId(catalog.getCatalogId());
//            catalog.setLabelList(labels);
//        }
//        //把数据放到redis里
//        redisTemplate.opsForValue().set("catalogList", catalogs);
//
//        //分页查询
//        return paging(pageNum, pageSize, catalogs);
//    }
//
//    //分页
//    private ApiReturnObject paging(Integer pageNum, Integer pageSize, List redisCatalogs) {
//        PageModel<Catalog> pageModel = new PageModel<>(redisCatalogs, pageSize);
//        List<Catalog> catalogList = pageModel.getObjects(pageNum);
//        //放置分页参数
//        Map<String, Object> map = new HashMap<>();
//        map.put("isHasPreviousPage", pageModel.isHasPreviousPage());//是否有上一页
//        map.put("isHasNextPage", pageModel.isHasNextPage());//是否有下一页
//        map.put("list", catalogList);
//        return new ApiReturnObject("200", "success", map);
//    }
//
//    //递归查询课程
//    public void recursive(Integer catalogId, List<Catalog> catalogs) {
//        List<Catalog> catalogList = catalogService.selectByParentId(catalogId);
//        for (Catalog catalog : catalogList) {
//            if (catalogList.size() != 0) {
//                //只有第三级才要添加进去
//                List<Catalog> list = catalogService.selectByParentId(catalog.getCatalogId());
//                if (list.size() == 0) {
//                     catalogs.add(catalog1);
//                } else {
//                    recursive(catalog.getCatalogId(), catalogs);
//                }
//            }
//        }
//    }


    /**
     * 猜你喜欢列表展示
     *
     * @throws Exception
     */
    @GetMapping("/userLike")
    @ResponseBody
    @ApiOperation("猜你喜欢列表展示")
    @ApiImplicitParam(name = "userId", value = "用户编号", dataType = "String")
    public void userLike(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("猜你喜欢列表展示", new Date()));
        List<CatalogUserLike> catalogUserLike = catalogUserLikeService.selectCatalogUserLikeByUserId(userId);
        List<Catalog> list = new ArrayList<>();
        if (catalogUserLike != null) {
            for (int i = 0; i < catalogUserLike.size(); i++) {
                Integer catalogId = catalogUserLike.get(i).getCatalogId();
                List<Catalog> list1 = catalogService.selectBySmall(catalogId);
                list.addAll(list1);
            }
            Collections.shuffle(list);
            writeJSON(list, response, "200", "success");
        }
    }

    /**
     * 考试左侧章节信息展示
     * 分级展示目录
     *
     * @throws Exception
     */
    @GetMapping("/catalogMenu")
    @ResponseBody
    @ApiOperation("分级展示目录")
    @ApiImplicitParam(name = "catalogId", value = "课程编号", dataType = "String")
    public void ksLeft(HttpServletResponse response, Integer catalogId) throws Exception {
        log.info(LogUtil.LogInfo("分级展示目录", new Date()));
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : list) {
            Integer catalogId1 = catalog.getCatalogId();
            List<Catalog> catalogs = catalogService.selectByParentId(catalogId1);
            catalog.setCatalogs(catalogs);
        }
        writeJSON(list, response, "200", "success");
    }

    /**
     * 查询专业大类
     *
     * @throws Exception
     */
    @GetMapping("/categories")
    @ResponseBody
    @ApiOperation("查询专业大类")
    public void ksLeft(HttpServletResponse response) throws Exception {
        log.info(LogUtil.LogInfo("查询专业大类", new Date()));
        List<Catalog> list = catalogService.selectCategoriesList();
        writeJSON(list, response, "200", "success");
    }

    /**
     * 查询专业大类
     *
     * @throws Exception
     */
    @GetMapping("/categoriesOne")
    @ResponseBody
    @ApiOperation("查询专业大类")
    public void categoriesOne(HttpServletResponse response) throws Exception {
        log.info(LogUtil.LogInfo("查询专业大类", new Date()));
        List<Catalog> list = catalogService.selectCategoriesList();
        if (list.size() > 0) {
            ArrayList<Catalog> list1 = Lists.newArrayList();
            //判断其下有没有第三级
            for (Catalog catalog : list) {
                //判断第二级
                List<Catalog> list2 = catalogService.selectByParentId(catalog.getCatalogId());
                if (list2.size() > 0) {
                    //存放二级下面所有三级课程
                    ArrayList<Catalog> arrayList = Lists.newArrayList();
                    //判断第三级
                    for (Catalog catalog1 : list2) {
                        List<Catalog> list3 = catalogService.selectByParentId(catalog1.getCatalogId());
                        arrayList.addAll(list3);
                    }
                    if (arrayList.size() == 0) list1.add(catalog);

                } else {
                    list1.add(catalog);
                }
            }
            //进行删除
            if (list1.size() > 0) {
                for (Catalog c : list1) {
                    list.remove(c);
                }
            }
        }
        writeJSON(list, response, "200", "success");
    }

    /**
     * 二级分类
     */
    @GetMapping("/categroiesTwo")
    public ApiReturnObject categroiesTwo(Integer catalogId) {
        //根据一级的id 查询出二级分类
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        return new ApiReturnObject("200", "查询成功", list);
    }


    /**
     * 三级课程
     */
    @GetMapping("/categroiesThree")
    public ApiReturnObject categroiesThree(Integer catalogId, Integer pageSize, Integer pageNum) {
        if (pageNum != null && pageSize != null) PageHelper.startPage(pageNum, pageSize);
        //根据一级的id 查询出二级分类
        List<Catalog> list = catalogService.selectByParentId(catalogId);
        PageInfo<Catalog> pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }


    /**
     * 查询首页4个模块
     *
     * @throws Exception
     */
    @GetMapping("/listForThree")
    @ResponseBody
    @ApiOperation("查询首页三个模块")
    public void listForThree2(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询热门课程", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("count", "4");
        List<Catalog> list1 = new ArrayList<>();
        List<Catalog> list = new ArrayList<>();
        List<Catalog> list2 = new ArrayList<>();
        //根据用户id查询用户信息

        userData data = userFeign.selectOneById(Integer.parseInt(userId));
        //查最新课程
        //1普通用户  
        //if (appLogin.getCustomerType().equals("1")) {
        //if (data.getCustomerType().equals("1")) {
        //查询目录等级是3的课程
        list = catalogService.newClass(map);
        for (Catalog catalog : list) {
            Map<String, Object> hashmap = new HashMap<>();
            //hashmap.put("userId", appLogin.getUserId());
            hashmap.put("userId", data.getUserId());
            hashmap.put("catalogId", catalog.getCatalogId());
            //查询该用户是否购买课程  已购买  就标红
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            //JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
                //catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }
        //}
        /* else {
            //2线下分配用户
            Map<String, Object> map2 = new HashMap<>();
            map2.put("schoolId", data.getSchoolId());
            map2.put("userType", "2");
            SysAppLogin login = loginService.selectByDeptId(map2);
            *//** 调用feign 返回用户信息 id *//*
            userData data1 = userData.builder().schoolId(data.getSchoolId()).sysType("2").build();
            Integer userId1 = userFeign.selectByDeptId(data1);

            list = catalogService.newClass(map);
            for (Catalog catalog : list) {
                Map<String, Object> hashmap = new HashMap<>();
                hashmap.put("userId", userId1);
                hashmap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                if (userCatalogBuy == null) {
                    catalog.setIsRed("0");
                    catalog.setCatalogBanner(null);
                } else {
                    catalog.setIsRed("1");
                }
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("userId", userId);
                objectMap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
                if (catalogBuy != null) {
                    catalog.setIsRed("1");
                }
            }
        }*/

        //查最热课程
        //1普通用户
        //if (data.getCustomerType().equals("1")) {
        list1 = catalogService.hotClass(map);
        for (Catalog catalog : list1) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", data.getUserId());
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
//                JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
//                    catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }
        //}
        /* else {
            //2线下分配用户
//            Map<String, Object> map2 = new HashMap<>();
//            map2.put("schoolId", data.getSchoolId());
//            map2.put("userType", "2");
//            SysAppLogin login = loginService.selectByDeptId(map2);
//            SysAppLogin login = loginService.selectByDeptId(map2);
            *//** 调用feign 返回用户信息 id *//*
            userData data1 = userData.builder().schoolId(data.getSchoolId()).sysType("2").build();
            Integer userId1 = userFeign.selectByDeptId(data1);

            list1 = catalogService.hotClass(map);
            for (Catalog catalog : list1) {
                Map<String, Object> hashmap = new HashMap<>();
                hashmap.put("userId", userId1);
                hashmap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                if (userCatalogBuy == null) {
                    catalog.setIsRed("0");
                    catalog.setCatalogBanner(null);
                } else {
                    catalog.setIsRed("1");
                }
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("userId", userId);
                objectMap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
                if (catalogBuy != null) {
                    catalog.setIsRed("1");
                }
            }
        }*/

        //查推荐
        //1普通用户
        //if (data.getCustomerType().equals("1")) {
        list2 = catalogService.selectList(map);
        for (Catalog catalog : list2) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", data.getUserId());
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
//                JrOrder jrOrder = orderService.selectJrOrderByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
//                    catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }
        //}
        /*else {
            //2线下分配用户
//            Map<String, Object> map2 = new HashMap<>();
//            map2.put("schoolId", data.getSchoolId());
//            map2.put("userType", "2");
//            SysAppLogin login = loginService.selectByDeptId(map2);

            *//** 调用feign 返回用户信息 id *//*
            userData data1 = userData.builder().schoolId(data.getSchoolId()).sysType("2").build();
            Integer userId1 = userFeign.selectByDeptId(data1);
            list2 = catalogService.selectList(map);
            for (Catalog catalog : list2) {
                Map<String, Object> hashmap = new HashMap<>();
                hashmap.put("userId", userId1);
                hashmap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
                if (userCatalogBuy == null) {
                    catalog.setIsRed("0");
                    catalog.setCatalogBanner(null);
                } else {
                    catalog.setIsRed("1");
                }
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("userId", userId);
                objectMap.put("catalogId", catalog.getCatalogId());
                UserCatalogBuy catalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(objectMap);
                if (catalogBuy != null) {
                    catalog.setIsRed("1");
                }
            }
        }*/

        //查询免费课程
        List<Map<String, Object>> list3 = new ArrayList<>();
        List<Microlesson> microlessons = microlessonService.selectMicrolessonList(null);
        for (Microlesson microlesson : microlessons) {
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("name", microlesson.getRName());
//            objectMap.put("catalogIco", microlesson.getCover());//图片
            objectMap.put("catalogIco", microlesson.getRUrl() + "?vframe/jpg/offset/1/w/480/h/360");//图片
            objectMap.put("price", "0");
            objectMap.put("catalogBanner", microlesson.getRUrl());//视频资源
            objectMap.put("catalognIntro", microlesson.getRRemark());//课程简介
            objectMap.put("catalogLearningNum", microlesson.getRBrowsing());//播放次数
            objectMap.put("id", microlesson.getId());//课程id
            objectMap.put("isRed", "2");//是否标红
            objectMap.put("RUrl", microlesson.getRUrl());//视频资源
            Bbs bbs = new Bbs();
            bbs.setMicrolessonId(microlesson.getId());
            List<Bbs> bbsList = bbsService.selectBbsList(bbs);
            objectMap.put("text", bbsList);//评论
            list3.add(objectMap);
        }

        List<Object> catalog = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map1.put("name", "推荐课程");
        map1.put("list", list2);
        map2.put("name", "最新课程");
        map2.put("list", list);
        map3.put("name", "最热课程");
        map3.put("list", list1);
        map4.put("name", "免费课程");
        map4.put("list", list3);

        catalog.add(map4);
        catalog.add(map3);
        catalog.add(map2);
        catalog.add(map1);
        writeJSON(catalog, response, "200", "success");
    }

    /**
     * 查询注册后课程筛选
     *
     * @throws Exception
     */
    @GetMapping("/catalogListsAndroid")
    @ResponseBody
    @ApiOperation("查询分类")
    public void catalogListsios(HttpServletResponse response, String userId) throws Exception {
        log.info(LogUtil.LogInfo("查询注册后课程筛选", new Date()));
        int size = 0;
        List<Catalog> list = catalogService.selectBig2();
        for (int i = 0; i < list.size(); i++) {
            List<Catalog> list2 = catalogService.selectBySmall2(list.get(i).getCatalogId());
            List<CatalogUserLike> catalogUserLikes = catalogUserLikeService.selectCatalogUserLikeByUserId(userId);
            if (catalogUserLikes.size() > 0) {
                for (Catalog listd2 : list2) {
                    for (CatalogUserLike catalogUserLike : catalogUserLikes) {
                        if (listd2.getCatalogId().intValue() == catalogUserLike.getCatalogId().intValue()) {
                            listd2.setIstrueflag(true);
                            break;
                        }
                    }
                }
            }
            size = catalogUserLikes.size();
            list.get(i).getCatalogs().addAll(list2);
        }
        writeJSON(list, response, "200", "" + size);
    }

    /**
     * 章节信息展示
     *
     * @throws Exception
     */
    @GetMapping("/listByCurrId")
    @ResponseBody
    @ApiOperation("章节信息展示")
    @ApiImplicitParam(name = "catalogId", value = "课程编号", dataType = "String")
    public void listByCurrId(HttpServletResponse response, Integer catalogId) throws Exception {
        log.info(LogUtil.LogInfo("章节信息展示", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("catalogId", catalogId);
        List<Catalog> list = catalogService.selectCatalogById(map);
        writeJSON(list, response, "200", "success");
    }

    /**
     * 查询课程详情
     *
     * @throws Exception
     */
    @GetMapping("/info")
    @ResponseBody
    @ApiOperation("查询课程详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catalogId", value = "目录编号", dataType = "Integer")})
    public void classInfo(Integer catalogId, Integer userId, HttpServletResponse response) throws Exception {
        log.info(LogUtil.LogInfo("查询课程详情", new Date()));
        Catalog catalog = catalogService.classInfo(catalogId);
        if (catalog.getCatalogLearningNum() == null) {
            catalog.setCatalogLearningNum(1);
        } else {
            catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
        }
        catalogService.updateCataLog(catalog);
        if (userId != null) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
                catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }
        writeJSON(catalog, response, "200", "success");
    }

    /**
     * 新增保存课后评价
     */
    @PostMapping("/addSave")
    @ApiOperation("发布课后评价")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "plText", value = "评论内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plCreateuser", value = "发布人", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plCurriculumid", value = "课程编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plImage", value = "评论图片", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plAttribute", value = "属性标签编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "plPraise", value = "1：好评，2：中评，3：差评", required = true, dataType = "String")})
    public void addSave(HttpServletResponse response, Comments comments) throws Exception {
        comments.setPlId(UUIDUtils.getByUUId());
        comments.setPlCreatetime(new Date());
        commentsService.insertComments(comments);
        writeJSON("添加成功", response, "200", "成功");
    }

    /**
     * 查询试卷列表
     */
    @GetMapping("/paperList")
    @ResponseBody
    @ApiOperation("查询小节下所有试卷信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "catalogId", value = "参数集合", dataType = "String"),})
    public void paperlist(HttpServletResponse response, String catalogId, Integer pageNo) throws Exception {
        log.info(LogUtil.LogInfo("小节下试卷信息", new Date()));
        Integer pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Paper> list = paperService.selectPaperByCatalog(catalogId);
        writeJSON(new PageInfo<>(list), response, "200", "成功");
    }

    /**
     * 虚拟教学
     */
    @GetMapping("/virtualTeaching")
    public void virtualTeaching(HttpServletResponse response, Integer catalogId, Integer pageNo) throws IOException {
        log.info(LogUtil.LogInfo("查询章节信息", new Date()));
        int pageSize = 10;
        PageHelper.startPage(pageNo, pageSize);
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog.setThreeD(resource.getrUrl());
                }
            }
        }
        writeJSON(new PageInfo<>(catalogs), response, "200", "success");
    }

    /**
     * 安卓查版本
     *
     * @param response
     * @param type
     * @throws IOException
     */
    @GetMapping("/queryVersion")
    public void queryVersion(HttpServletResponse response, Integer type) throws IOException {
        String version = versionMapper.queryVersion(type);
        writeJSON(version, response, "200", "success");
    }

    /**
     * 公共的虚拟教学和实操视频
     */
    @GetMapping("/otherOne")
    public ApiReturnObject otherOne(Integer catalogId, Integer pageNo) {
        //实操视频
        Map<String, Object> videoMap = new HashMap<>();
        videoMap.put("rCatalogid", catalogId);
        videoMap.put("type", "4");
        List<Resources> videoList = resourcesService.selectInfoByType(videoMap);
//        if(pageNo != null){
//            int pageSize = 10; PageHelper.startPage(pageNo, pageSize);
//        }
//        PageInfo<Resources> videoPage = new PageInfo<>(videoList);

        //虚拟教学
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog1 : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog1.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog1.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog1.setThreeD(resource.getrUrl());
                }
            }
        }
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("videoPage", videoList);
        map.put("catalogs", catalogs);
        return new ApiReturnObject("200", "查询成功", map);
    }

    /**
     * 公共的教学课件和技术文档
     */
    @GetMapping("/otherTwo")
    public ApiReturnObject otherTwo(Integer catalogId, Integer pageNo) {
        //教学课件
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("rCatalogid", catalogId);
        courseMap.put("type", "1");
        List<Resources> courseList = resourcesService.selectInfoByType(courseMap);
        //if(pageNo != null){
        //    int pageSize = 10; PageHelper.startPage(pageNo, pageSize);
        //}
        //PageInfo<Resources> coursePage = new PageInfo<>(courseList);

        //技术文档
        Map<String, Object> wordMap = new HashMap<>();
        wordMap.put("rCatalogid", catalogId);
        wordMap.put("type", "3");
        List<Resources> wordList = resourcesService.selectInfoByType(wordMap);
        //if(pageNo != null){
        //    int pageSize = 10; PageHelper.startPage(pageNo, pageSize);
        //}
        //PageInfo<Resources> wordPage = new PageInfo<>(wordList);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("coursePage", courseList);
        map.put("wordPage", wordList);
        return new ApiReturnObject("200", "查询成功", map);
    }

    /**
     * 课程详情
     * 实操视频    虚拟教学
     * 实战照片
     * 教学课件    技术文档
     * 章节测试
     *
     * @param catalogId
     * @param userId
     * @param pageNo
     * @return
     */
    @GetMapping("/resAll")
    public ApiReturnObject resAll(Integer catalogId, Integer userId, Integer pageNo) {
        //课程详情
        Catalog catalog = catalogService.classInfo(catalogId);
        if (catalog.getCatalogLearningNum() == null) {
            catalog.setCatalogLearningNum(1);
        } else {
            catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
        }
        catalogService.updateCataLog(catalog);
        if (userId != null) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
                catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }

        //实操视频
        Map<String, Object> videoMap = new HashMap<>();
        videoMap.put("rCatalogid", catalogId);
        videoMap.put("type", "4");
        List<Resources> videoList = resourcesService.selectInfoByType(videoMap);
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        PageInfo<Resources> videoPage = new PageInfo<>(videoList);

        //虚拟教学
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog1 : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog1.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog1.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog1.setThreeD(resource.getrUrl());
                }
            }
        }

        //实战照片
        Map<String, Object> imgMap = new HashMap<>();
        imgMap.put("rCatalogid", catalogId);
        imgMap.put("type", "5");
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Resources> imgList = resourcesService.selectInfoByType(imgMap);
        PageInfo<Resources> imgPage = new PageInfo<>(imgList);

        //教学课件
        Map<String, Object> courseMap = new HashMap<>();
        courseMap.put("rCatalogid", catalogId);
        courseMap.put("type", "1");
        List<Resources> courseList = resourcesService.selectInfoByType(courseMap);
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        PageInfo<Resources> coursePage = new PageInfo<>(courseList);

        //技术文档
        Map<String, Object> wordMap = new HashMap<>();
        wordMap.put("rCatalogid", catalogId);
        wordMap.put("type", "3");
        List<Resources> wordList = resourcesService.selectInfoByType(wordMap);
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        PageInfo<Resources> wordPage = new PageInfo<>(wordList);

        //试卷列表
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Paper> paperList = paperService.selectPaperByCatalog(catalogId.toString());
        for (Paper paper : paperList) {
            Date createTime = paper.getCreateTime();
            String date = DateUtil.formatDateTime(createTime);
            paper.setCreateTimeStr(date);
        }
        PageInfo<Paper> paperPageInfo = new PageInfo<>(paperList);

        HashMap<String, Object> map = Maps.newHashMap();
        //课程详情
        map.put("catalog", catalog);
        //技术文档
        map.put("wordPage", wordPage);
        //实操视频
        map.put("videoPage", videoPage);
        //虚拟教学
        map.put("catalogs", catalogs);
        //实战照片
        map.put("imgPage", imgPage);
        //教学课件
        map.put("coursePage", coursePage);
        //试卷列表
        map.put("paperList", paperPageInfo);
        return new ApiReturnObject("200", "查询成功", map);
    }

    /**
     * 实操视频 虚拟教学
     */
    @GetMapping("/resOtherOne")
    public ApiReturnObject resOtherOne(Integer catalogId, Integer userId, Integer pageNo) {
        //课程详情
        Catalog catalog = catalogService.classInfo(catalogId);
        if (catalog.getCatalogLearningNum() == null) {
            catalog.setCatalogLearningNum(1);
        } else {
            catalog.setCatalogLearningNum(catalog.getCatalogLearningNum() + 1);
        }
        catalogService.updateCataLog(catalog);
        if (userId != null) {
            Map<String, Object> hashmap = new HashMap<>();
            hashmap.put("userId", userId);
            hashmap.put("catalogId", catalog.getCatalogId());
            UserCatalogBuy userCatalogBuy = userCatalogBuyService.selectUserCatalogBuyByUserId(hashmap);
            if (userCatalogBuy == null) {
                catalog.setIsRed("0");
                catalog.setCatalogBanner(null);
            } else {
                catalog.setIsRed("1");
            }
        }

        //实操视频
        Map<String, Object> videoMap = new HashMap<>();
        videoMap.put("rCatalogid", catalogId);
        videoMap.put("type", "4");
        List<Resources> videoList = resourcesService.selectInfoByType(videoMap);
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        PageInfo<Resources> videoPage = new PageInfo<>(videoList);


        //虚拟教学
        if (pageNo != null) {
            int pageSize = 10;
            PageHelper.startPage(pageNo, pageSize);
        }
        List<Catalog> catalogs = catalogService.selectByParentId(catalogId);
        for (Catalog catalog1 : catalogs) {
            Map<String, Object> map = new HashMap<>();
            map.put("rCatalogid", catalog1.getCatalogId());
            List<Resources> resources = resourcesService.selectVideoById(map);
            for (Resources resource : resources) {
                if (resource.getType().equals("9")) {
                    catalog1.setFlash(resource.getrUrl());
                } else if (resource.getType().equals("8")) {
                    catalog1.setThreeD(resource.getrUrl());
                }
            }
        }

        HashMap<String, Object> map = Maps.newHashMap();
        //课程详情
        map.put("catalog", catalog);
        //实操视频
        map.put("videoPage", videoPage);
        //虚拟教学
        map.put("catalogs", catalogs);
        return new ApiReturnObject("200", "查询成功", map);
    }


    /** 教学课件 技术文档 */


    /**
     * 购买课程时详情页数据的展示    课程图片 课程名称 创建日期 达人币 优惠金额 达人币余额 是否有足够达人币进行购买
     */
    @GetMapping("/selectOneToBuy")
    private ApiReturnObject selectOneToBuy(Integer catalogId, Integer userId) {
        //查询课程信息
        Catalog catalog = catalogService.selectCatalogByCatalogIdToBuy(catalogId);
        //查询用户信息 判断达人币是否充足
        Map<String, Object> map = userFeign.selectByUserId(userId);
        if (map.size() == 0) {
            return new ApiReturnObject("201", "userId有误", catalog);
        }
        BigDecimal price = new BigDecimal(map.get("price").toString());
        //catalog.setBalance((Integer) map.get("price"));
        catalog.setBalance(price);
        catalog.setReduced(0);
        //if (catalog.getPrice() > price) {
        if (new BigDecimal(catalog.getPrice()).compareTo(price) > 0) {
            //达人币不足
            catalog.setIsEnough("2");
        } else {
            catalog.setIsEnough("1");
        }
        Date createTime = catalog.getCreateTime();
        if (StringUtils.isNull(createTime)) {
            createTime = new Date();
        }
        String s = DateUtils.dateTime(createTime);
        catalog.setCreateTimeStr(s);
        return new ApiReturnObject("200", "查询成功", catalog);
    }


}
